<?php

namespace Home\Controller;

/**
 * 鞋品信息
 */
class ShoseController extends CommonController {
    /**
     * 鞋品首页
     * @return
     */
    public function index() {
        $shoses = D('Shose')->relation(true)->order('id DESC')->select();

        $this->assign('shoses', $shoses);
        $this->display();
    }

    /**
     * 最新推荐
     * @return
     */
    public function recommend() {
        $shoses = D('Shose')->relation(true)
                           ->order('created_at DESC')
                           ->limit(5)
                           ->select();

        // $dishes = array();

        // foreach ($names as $name) {
        //     $item = D('Dishes')->relation(true)
        //                        ->where("name='{$name}'")
        //                        ->find();

        //     if (!is_null($item)) {
        //         $dishes[] = $item;
        //     }
        // }

        $this->assign('shoses', $shoses);
        $this->display();
    }

    /**
     * 菜品分类
     * @return
     */
    public function category() {
        $categories = M('Category')->select();

        $this->assign('categories', $categories);
        $this->display();
    }

    /**
     * 菜品分类页
     * @return
     */
    public function categoryPage() {
        $category = D('Category')->relation(true)->getById($_GET['id']);

        if (empty($category)) {
            $category = D('Category')->relation(true)->order('id DESC')->find();
        }

        $this->assign('category', $category);
        $this->display('category_page');
    }

    /**
     * 菜品信息
     * @return
     */
    public function show() {
        $shose = D('Shose')->relation(true)
                             ->where("uuid='{$_GET['uuid']}'")
                             ->find();

        // 不存在则选取最新的一条
        if (empty($shose)) {
            $shose = D('Shose')->relation(true)->order('id DESC')->find();
        }

        $this->assign('shose', $shose);
        $this->display();
    }

    /**
     * 菜品评论
     * @return
     */
    public function comment() {
        $shose = M('Shose')->getById($_GET['shose_id']);

        if (empty($shose)) {
            return $this->error('菜品不存在！');
        }

        $comments = M('Comment')->where("shose_id={$shose['id']}")->order('id DESC')->select();

        foreach ($comments as $key => $comment) {
            $comments[$key]['content'] =
                str_replace("\n", '<br/>', $comment['content']);
        }

        $this->assign('comments', $comments);
        $this->display();
    }

    /**
     * 添加评论
     * @return
     */
    public function doComment() {
        $Comment = D('Comment');

        $comment = $Comment->create($_POST['comment']);
        if (false === $Comment->add($comment)) {
            return $this->ajaxReturn(array('status' => false,
                                           'info' => '评论失败！'));
        }

        $this->ajaxReturn(array('status' => true,
                                'info' => '评论成功！'));
    }

    public function doSearch() {
        if (empty($_GET['search_type'])) {
            return $this->redirect('Index/index');
        }

        $where = array();
        if (1 == $_GET['search_type']) {
            $name = empty($_GET['type']) ? '' : $_GET['type'];
            $where['name'] = array('like', "%{$name}%");
            $categories = D('Category')->relation(true)
                                       ->where($where)
                                       ->select();
            $shoses = array();
            foreach ($categories as $category) {
                foreach ($category['shoses'] as $key => $shose) {
                    $shose['category']['name'] = $category['name'];
                    $shoses[] = $shose;
                }
            }
        } else if (2 == $_GET['search_type']) {
            $where['name'] = array('like', "%{$_GET['name']}%");
            $shoses = D('Shose')->relation(true)->where($where)->select();
        } else if (3 == $_GET['search_type']) {
            if (is_numeric($_GET['price'])) {
                $where['price'] = array(
                    array('gt', $_GET['price'] - 50),
                    array('lt', $_GET['price'] + 50),
                    'AND'
                );
            }

            $shoses = D('Shose')->relation(true)->where($where)->select();
        }

        $this->assign('shoses', $shoses);
        $this->display('search');
    }
}
